﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using GameLib.Main;
using Sandbox.Editor.Modes.ModePanels;
using Sandbox.Scene.Elements;
using System.Windows.Controls;
using System.Windows;
using System.Windows.Input;
using System.Windows.Shapes;
using System.Windows.Media;

namespace Sandbox.Editor.Modes
{
	public class RefImageMode : EditorMode
	{
		public RefImageMode(EditorWindow window)
			: base(window)
		{
			Panel.Mode = this;
		}

		public void OnAddingImage(string fileName)
		{
			Uri uri = new Uri(System.IO.Path.GetFullPath(fileName));
			Uri appUri = new Uri(System.IO.Path.GetFullPath("./"));
			Uri relUri = appUri.MakeRelativeUri(uri);
			Window.MapScene.AddReferenceImage(relUri);
			RefreshImageList();
			Panel.ListBoxImage.SelectedIndex = Panel.ListBoxImage.Items.Count - 1;
		}

		public void OnImageSelectionChanged(SelectionChangedEventArgs e)
		{
			if (Panel.ListBoxImage.SelectedItem != null)
			{
				Panel.SliderOpacity.Value = (Panel.ListBoxImage.SelectedItem as ReferenceImage).Opacity;
			}
		}

		public void OnOpacitySliderChanged(RoutedPropertyChangedEventArgs<double> e)
		{
			if (Panel.ListBoxImage.SelectedItem != null)
			{
				Panel.SliderOpacity.Value = (Panel.ListBoxImage.SelectedItem as ReferenceImage).Opacity = Panel.SliderOpacity.Value;
			}
		}

		public override void Begin()
		{
			base.Begin();
			ShowGuideLines();

			int lastSel = Panel.ListBoxImage.SelectedIndex;
			RefreshImageList();
			Panel.ListBoxImage.SelectedIndex = Panel.ListBoxImage.Items.Count > 0 ? (lastSel >= 0 ? lastSel : 0) : -1;
		}

		public override void End()
		{
			base.End();
		}

		public override void MapWindow_MouseLeftButtonDown(MouseButtonEventArgs e)
		{
			base.MapWindow_MouseLeftButtonDown(e);
			if (!IsDraggingImage && Panel.ListBoxImage.SelectedItem != null)
			{
				IsDraggingImage = true;
				LastDraggingPoint = e.GetPosition(Window.MapWindow);
			}
		}

		public override void MapWindow_MouseLeftButtonUp(MouseButtonEventArgs e)
		{
			base.MapWindow_MouseLeftButtonUp(e);
			IsDraggingImage = false;
		}

		public override void MapWindow_MouseMove(MouseEventArgs e)
		{
			base.MapWindow_MouseMove(e);

			if (IsDraggingImage && Panel.ListBoxImage.SelectedItem != null)
			{
				Point new_point = e.GetPosition(Window.MapWindow);
				(Panel.ListBoxImage.SelectedItem as ReferenceImage).Scroll(
					new_point.X - LastDraggingPoint.X, 
					new_point.Y - LastDraggingPoint.Y);
				LastDraggingPoint = new_point;
			}
			else
			{
				IsDraggingImage = false;
			}
		}

		public override void MapWindow_LoseFocus(MouseEventArgs e)
		{
			base.MapWindow_LoseFocus(e);
			IsDraggingImage = false;
		}

		public override void MapWindow_MouseWheel(MouseWheelEventArgs e)
		{
			base.MapWindow_MouseWheel(e);
			if (Panel.ListBoxImage.SelectedItem != null)
			{
				(Panel.ListBoxImage.SelectedItem as ReferenceImage).Scale(e.Delta * 0.0005);
			}
		}

		private void RefreshImageList()
		{
			Panel.ListBoxImage.Items.Clear();

			foreach (ReferenceImage refImage in Window.MapScene.ReferenceImages)
			{
				Panel.ListBoxImage.Items.Add(refImage);
			}
		}

		private void ShowGuideLines()
		{
			if (GuideLinePanel == null)
			{
				GuideLinePanel = new Grid();
				GuideLinePanel.Margin = new Thickness(20, 0, 0, 20);
				GuideLinePanel.VerticalAlignment = VerticalAlignment.Bottom;
				GuideLinePanel.HorizontalAlignment = HorizontalAlignment.Left;
				GuideLinePanel.Width = 300;
				GuideLinePanel.Height = 300;

				Rectangle rect1 = new Rectangle();
				rect1.Stroke = new SolidColorBrush(Colors.Red);
				rect1.Width = Window.MapScene.HexHeightScaled;
				rect1.Height = Window.MapScene.HexWidthScaled;
				rect1.HorizontalAlignment = HorizontalAlignment.Left;
				rect1.VerticalAlignment = VerticalAlignment.Bottom;
				GuideLinePanel.Children.Add(rect1);

				Rectangle rect2 = new Rectangle();
				rect2.Margin = new Thickness(Window.MapScene.HexHeightScaled, 0, 0, 0);
				rect2.Stroke = new SolidColorBrush(Colors.Red);
				rect2.Width = Window.MapScene.HexHeightScaled;
				rect2.Height = Window.MapScene.HexWidthScaled;
				rect2.HorizontalAlignment = HorizontalAlignment.Left;
				rect2.VerticalAlignment = VerticalAlignment.Bottom;
				GuideLinePanel.Children.Add(rect2);

				Window.MapWindow.Children.Add(GuideLinePanel);

			}

			GuideLinePanel.Visibility = Visibility.Visible;
		}

		private RefImagePanel Panel
		{
			get { return Window.RefImagePanel; }
		}

		private bool IsDraggingImage = false;
		private Point LastDraggingPoint;
		private Grid GuideLinePanel;
	}
}
